package com.hao.netty.chapter02.bio;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description
 *  同步的阻塞服务器
 *  这种模式，当大量的IO请求过来的时候，服务器端就需要开启大量的线程，对服务器的压力非常大
 * @Date 2020/1/11
 * @Author songhao
 **/
public class TimeServer {

    public static void main(String[] args) throws Exception{
        int port = 8080;
        if(args != null && args.length > 0){
            try{
                port = Integer.valueOf(args[0]);
            }catch (Exception e){
                port = 8080;
            }
        }
        ServerSocket server = null;
        try{
            server = new ServerSocket(port);
            System.out.println("The time server is start in port:" + port);
            Socket socket = null;
            while(true){
                socket = server.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (server != null){
                System.out.println("The time server close");
                server.close();
                server = null;
            }
        }
    }

}
